package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeRespE;
import com.my.zl.console.dao.autocode.ZlKeywordDtlMapper;
import com.my.zl.console.dao.autocode.ZlKeywordMapper;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordAddDTO;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordDelDTO;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordDetailDTO;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordEditDTO;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordQueryDTO;
import com.my.zl.console.dto.autocode.keyword.ReqKeywordStateDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordAddDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordDelDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordDetailDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordEditDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordQueryDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordStateDTO;
import com.my.zl.console.dto.autocode.keyword.ResKeywordVO2;
import com.my.zl.console.entity.autocode.ZlKeyword;
import com.my.zl.console.po.autocode.keyword.ZlKeywordVo;
import com.my.zl.console.service.autocode.IKeywordService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 数据库关键字表
 * @author ZL
 */
@Service
public class KeywordServiceImpl extends BaseManager implements IKeywordService {

    @Autowired
    private ZlKeywordMapper keywordMapper;
    @Autowired
    private ZlKeywordDtlMapper keywordDtlMapper;

    @Override
    public ResKeywordQueryDTO queryKeywordList(ReqKeywordQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResKeywordQueryDTO resDTO = BeanUtil.copyProperties(page, ResKeywordQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlKeywordVo> list = keywordMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResKeywordVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResKeywordDetailDTO queryKeywordDetail(ReqKeywordDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResKeywordDetailDTO res = new ResKeywordDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlKeyword view = keywordMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResKeywordAddDTO insertKeywordInfo(ReqDTO<ReqKeywordAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordAddDTO reqData = reqInfo.getReqData();
        ResKeywordAddDTO res = new ResKeywordAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlKeyword record = new ZlKeyword();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setKeywordId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordId",record.getKeywordId());
        //检查主键是否冲突
        if(keywordMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        keywordMapper.insertSelective(record);
        
        ZlKeyword keyInfo = keywordMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordEditDTO updateKeywordInfo(ReqDTO<ReqKeywordEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordEditDTO reqData = reqInfo.getReqData();
        ResKeywordEditDTO res = new ResKeywordEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlKeyword record = new ZlKeyword();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        keywordMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordId",record.getKeywordId());
        ZlKeyword keyInfo = keywordMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordDelDTO deleteKeywordInfo(ReqDTO<ReqKeywordDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDelDTO reqData = reqInfo.getReqData();
        ResKeywordDelDTO res = new ResKeywordDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordId",reqData.getKeywordId());
        if(keywordDtlMapper.countExistKey(paramMap) > 0){
            throw new BizException(AutocodeRespE.DELETE_NOT_ALLOWED);
        }
        
        paramMap = BeanUtil.beanToMap(reqData);
        keywordMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResKeywordStateDTO updateKeywordState(ReqDTO<ReqKeywordStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordStateDTO reqData = reqInfo.getReqData();
        ResKeywordStateDTO res = new ResKeywordStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlKeyword record = new ZlKeyword();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        keywordMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordId",record.getKeywordId());
        ZlKeyword keyInfo = keywordMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}